package cn.iocoder.yudao.module.carbon.service.emissionfactor;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.carbon.controller.admin.emissionfactor.vo.EmissionFactorCreateReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.emissionfactor.vo.EmissionFactorExportReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.emissionfactor.vo.EmissionFactorPageReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.emissionfactor.vo.EmissionFactorUpdateReqVO;
import cn.iocoder.yudao.module.carbon.convert.emissionfactor.EmissionFactorConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.emissionfactor.EmissionFactorDO;
import cn.iocoder.yudao.module.carbon.dal.mysql.emissionfactor.EmissionFactorMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.carbon.enums.ErrorCodeConstants.EMISSION_FACTOR_NOT_EXISTS;

/**
 * 碳排放因子 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class EmissionFactorServiceImpl implements EmissionFactorService {

    @Resource
    private EmissionFactorMapper emissionFactorMapper;

    @Override
    public Integer createEmissionFactor(EmissionFactorCreateReqVO createReqVO) {
        // 插入
        EmissionFactorDO emissionFactor = EmissionFactorConvert.INSTANCE.convert(createReqVO);
        emissionFactorMapper.insert(emissionFactor);
        // 返回
        return emissionFactor.getId();
    }

    @Override
    public void updateEmissionFactor(EmissionFactorUpdateReqVO updateReqVO) {
        // 校验存在
        validateEmissionFactorExists(updateReqVO.getId());
        // 更新
        EmissionFactorDO updateObj = EmissionFactorConvert.INSTANCE.convert(updateReqVO);
        emissionFactorMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmissionFactor(Integer id) {
        // 校验存在
        validateEmissionFactorExists(id);
        // 删除
        emissionFactorMapper.deleteById(id);
    }

    private void validateEmissionFactorExists(Integer id) {
        if (emissionFactorMapper.selectById(id) == null) {
            throw exception(EMISSION_FACTOR_NOT_EXISTS);
        }
    }

    @Override
    public EmissionFactorDO getEmissionFactor(Integer id) {
        return emissionFactorMapper.selectById(id);
    }

    @Override
    public List<EmissionFactorDO> getEmissionFactorList(Collection<Integer> ids) {
        return emissionFactorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<EmissionFactorDO> getEmissionFactorPage(EmissionFactorPageReqVO pageReqVO) {
        return emissionFactorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EmissionFactorDO> getEmissionFactorList(EmissionFactorExportReqVO exportReqVO) {
        return emissionFactorMapper.selectList(exportReqVO);
    }

}
